﻿using System;
using System.Collections.Generic;

namespace UniRx
{
	// Token: 0x0200022A RID: 554
	public class MessageBroker : IMessageBroker, IDisposable, IMessagePublisher, IMessageReceiver
	{
		// Token: 0x06000D01 RID: 3329 RVA: 0x0003EC04 File Offset: 0x0003D004
		public void Publish<T>(T message)
		{
			object obj = this.notifiers;
			object obj2;
			lock (obj)
			{
				if (this.isDisposed)
				{
					return;
				}
				if (!this.notifiers.TryGetValue(typeof(T), out obj2))
				{
					return;
				}
			}
			((ISubject<T>)obj2).OnNext(message);
		}

		// Token: 0x06000D02 RID: 3330 RVA: 0x0003EC78 File Offset: 0x0003D078
		public IObservable<T> Receive<T>()
		{
			object obj = this.notifiers;
			object obj2;
			lock (obj)
			{
				if (this.isDisposed)
				{
					throw new ObjectDisposedException("MessageBroker");
				}
				if (!this.notifiers.TryGetValue(typeof(T), out obj2))
				{
					ISubject<T> subject = new Subject<T>().Synchronize<T>();
					obj2 = subject;
					this.notifiers.Add(typeof(T), obj2);
				}
			}
			return ((IObservable<T>)obj2).AsObservable<T>();
		}

		// Token: 0x06000D03 RID: 3331 RVA: 0x0003ED10 File Offset: 0x0003D110
		public void Dispose()
		{
			object obj = this.notifiers;
			lock (obj)
			{
				if (!this.isDisposed)
				{
					this.isDisposed = true;
					this.notifiers.Clear();
				}
			}
		}

		// Token: 0x04000753 RID: 1875
		public static readonly IMessageBroker Default = new MessageBroker();

		// Token: 0x04000754 RID: 1876
		private bool isDisposed;

		// Token: 0x04000755 RID: 1877
		private readonly Dictionary<Type, object> notifiers = new Dictionary<Type, object>();
	}
}
